package com.fs.user.util;


import com.google.code.kaptcha.Constants;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/**
 * Author:HOU
 * date:2023/1/2  21:35
 * 案例:
 **/
public class UserContext {

    private static final String USER_IN_SESSION="loginUser";
    public static HttpSession getSession(){
        //Spring提供了RequestContextHolder类, 提供public static RequestAttributes getRequestAttributes()
        //RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //得到RequestAttributes对象 转换ServletRequestAttributes(子类)
        //ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)requestAttributes;
        //ServletRequestAttributes 得到Request,Response,Session对象
        //return servletRequestAttributes.getRequest().getSession();

        //点运算符 优先级高于()
        return ((ServletRequestAttributes)(RequestContextHolder.getRequestAttributes()))
                .getRequest().getSession();
    }

    public static String getVerifyCode(){
        Object attribute = getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        return attribute==null?null:(String) attribute;
    }

//    public static void setLoginUser(Admin admin){
//        getSession().setAttribute(USER_IN_SESSION,admin);
//
//    }
//
//    public static Admin getLoginUser(){
//        Object attribute = getSession().getAttribute(USER_IN_SESSION);
//        return attribute==null?null:(Admin) attribute;
//
//    }

}
